home *** CD-ROM | disk | FTP | other *** search
/ Shirley Barber's Fairies: Create, Paint & Play / Fairies.iso / pc / Files / Intro.dxr / Internal_3_Loop for X Seconds.ls < prev    next >
Encoding:
Text File  |  2003-10-03  |  6.9 KB  |  177 lines

  1. property myTimeOut, myTimeUnit, myTimeOutFrame, myImmediateJump, myStartFrame, myStartTicks, myEndFrame
  2.  
  3. on getBehaviorDescription me
  4.   return "LOOP FOR X SECONDS: FRAME BEHAVIOR" & RETURN & RETURN & "This behavior will make the playback head loop for a fixed length of time over a certain number of frames, then jump to a chosen marker. " & "You can choose whether the playback head jumps immediately at the end of the period, or whether it should run right through to the last frame of the span." & RETURN & RETURN & "Drag this behavior to the frame channel of the Score Window, then stretch it out over the frames you wish to loop over. " & "If you wish to remain on the same frame, then simply do not stretch out the sprite." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "Frame behavior" & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Duration of loop (1 tick - 120 hours)" & RETURN & "* Marker to jump to at the end of the period" & RETURN & "* Playback head jumps immediately | at the end of the cycle"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "Frame behavior. " & "Stretch this behavior over a sequence of frames to make the playback head loop for a fixed length of time then jump to a chosen marker. " & "Option: ensure that the cycle is fully completed before the playback head jumps."
  9. end
  10.  
  11. on beginSprite me
  12.   Initialize(me)
  13.   updateStage()
  14. end
  15.  
  16. on exitFrame me
  17.   CheckTimeOut(me)
  18. end
  19.  
  20. on Initialize me
  21.   thisSprite = sprite(the currentSpriteNum)
  22.   myStartFrame = thisSprite.startFrame
  23.   myEndFrame = thisSprite.endFrame
  24.   if symbolp(myTimeOutFrame) then
  25.     case myTimeOutFrame of
  26.       #previous:
  27.         jumpToFrame = the frame - 1
  28.       #loop:
  29.         jumpToFrame = the frame
  30.       #next:
  31.         jumpToFrame = the frame + 1
  32.     end case
  33.   else
  34.     jumpToFrame = marker(myTimeOutFrame)
  35.   end if
  36.   if the currentSpriteNum then
  37.     ErrorAlert(me, #invalidChannel, the currentSpriteNum)
  38.   end if
  39.   if not jumpToFrame then
  40.     jumpToFrame = myEndFrame + 1
  41.     ErrorAlert(me, #missingMarker, jumpToFrame)
  42.   else
  43.     if (jumpToFrame >= myStartFrame) and (jumpToFrame <= myEndFrame) then
  44.       jumpToFrame = myEndFrame + 1
  45.       ErrorAlert(me, #endlessLoop, jumpToFrame)
  46.     end if
  47.   end if
  48.   myStartTicks = the ticks
  49.   case myTimeUnit of
  50.     "Seconds":
  51.       myTimeOut = myTimeOut * 60
  52.     "Minutes":
  53.       myTimeOut = myTimeOut * 60 * 60
  54.     "Hours":
  55.       myTimeOut = myTimeOut * 60 * 60 * 60
  56.   end case
  57.   myTimeOut = myTimeOut + myStartTicks
  58.   myTimeOutFrame = jumpToFrame
  59.   myImmediateJump = myImmediateJump = "jump immediately"
  60. end
  61.  
  62. on CheckTimeOut me
  63.   if the ticks > myTimeOut then
  64.     if myImmediateJump or (the frame = myEndFrame) then
  65.       go(myTimeOutFrame)
  66.     end if
  67.   else
  68.     if the frame = myEndFrame then
  69.       go(myStartFrame)
  70.     end if
  71.   end if
  72. end
  73.  
  74. on substituteStrings me, parentString, childStringList
  75.   i = childStringList.count()
  76.   repeat while i
  77.     tempString = EMPTY
  78.     dummyString = childStringList.getPropAt(i)
  79.     replacement = childStringList[i]
  80.     lengthAdjust = dummyString.char.count - 1
  81.     repeat while 1
  82.       position = offset(dummyString, parentString)
  83.       if not position then
  84.         parentString = tempString & parentString
  85.         exit repeat
  86.         next repeat
  87.       end if
  88.       if position <> 1 then
  89.         tempString = tempString & parentString.char[1..position - 1]
  90.       end if
  91.       tempString = tempString & replacement
  92.       delete parentString.char[1..position + lengthAdjust]
  93.     end repeat
  94.     i = i - 1
  95.   end repeat
  96.   return parentString
  97. end
  98.  
  99. on ErrorAlert me, theError, Data
  100.   behaviorName = string(me)
  101.   delete word 1 of behaviorName
  102.   delete char -30001 of behaviorName
  103.   delete char -30001 of behaviorName
  104.   case theError of
  105.     #invalidChannel:
  106.       if the runMode = "Author" then
  107.         terror1 = "BEHAVIOR ERROR: Frame ^0, Sprite ^1"
  108.         terror1 = substituteStrings(me, terror1, ["^0": the frame, "^1": the currentSpriteNum])
  109.         terror2 = "Behavior ^0 should be attached to the frame script channel."
  110.         terror2 = substituteStrings(me, terror2, ["^0": behaviorName])
  111.         terror3 = "Current Channel = ^0"
  112.         terror3 = substituteStrings(me, terror3, ["^0": Data])
  113.         alert(terror1 & RETURN & RETURN & terror2 & RETURN & RETURN & terror3)
  114.         abort()
  115.       end if
  116.     #missingMarker:
  117.       if the runMode = "Author" then
  118.         terror1 = "BEHAVIOR ERROR: Frame ^0, Sprite ^1"
  119.         terror1 = substituteStrings(me, terror1, ["^0": the frame, "^1": the currentSpriteNum])
  120.         terror2 = "Frame behavior ^0 is set to jump to marker ^1. " & "This marker cannot be found. " & " Choose a valid marker in the Behavior Parameters dialog."
  121.         terror2 = substituteStrings(me, terror2, ["^0": behaviorName, "^1": myTimeOutFrame])
  122.         terror3 = "In the meantime, frame ^0 will be used instead."
  123.         terror3 = substituteStrings(me, terror3, ["^0": Data])
  124.         alert(terror1 & RETURN & RETURN & terror2 & RETURN & RETURN & terror3)
  125.       end if
  126.     #endlessLoop:
  127.       if the runMode = "Author" then
  128.         if symbolp(myTimeOutFrame) then
  129.           case myTimeOutFrame of
  130.             #previous:
  131.               jumpToFrame = marker(-1)
  132.             #loop:
  133.               jumpToFrame = marker(0)
  134.             #next:
  135.               jumpToFrame = marker(1)
  136.           end case
  137.         else
  138.           jumpToFrame = marker(myTimeOutFrame)
  139.         end if
  140.         terror1 = "BEHAVIOR ERROR: Frame ^0, Sprite ^1"
  141.         terror1 = substituteStrings(me, terror1, ["^0": the frame, "^1": the currentSpriteNum])
  142.         terror2 = "Frame behavior ^0 is set to jump to marker ^1 (frame ^2 ). " & "  This is within the span of the behavior and will cause an endless loop."
  143.         terror2 = substituteStrings(me, terror2, ["^0": behaviorName, "^1": myTimeOutFrame, "^2": jumpToFrame])
  144.         terror3 = "Frame ^0 will be used instead."
  145.         terror3 = substituteStrings(me, terror3, ["^0": Data])
  146.         alert(terror1 & RETURN & RETURN & terror2 & RETURN & RETURN & terror3)
  147.       end if
  148.   end case
  149. end
  150.  
  151. on isOKToAttach me, aSpriteType, aSpriteNum
  152.   tisok = 0
  153.   if aSpriteType = #script then
  154.     tisok = 1
  155.   end if
  156.   return tisok
  157. end
  158.  
  159. on getPropertyDescriptionList me
  160.   nextMarker = nextMarker(me)
  161.   return [#myTimeOut: [#comment: "Loop over selected frames for...", #format: #integer, #range: [#min: 1, #max: 120], #default: 30], #myTimeUnit: [#comment: EMPTY, #format: #string, #range: ["Ticks", "Seconds", "Minutes", "Hours"], #default: "Seconds"], #myTimeOutFrame: [#comment: "... " & "then jump to marker", #format: #marker, #default: nextMarker], #myImmediateJump: [#comment: "When the time is up", #format: #string, #range: ["complete the loop", "jump immediately"], #default: "jump immediately"]]
  162. end
  163.  
  164. on nextMarker me
  165.   labelString = the labelList
  166.   delete char -30000 of labelString
  167.   markerCount = the number of lines in labelString
  168.   theFrame = the frame
  169.   repeat with i = 1 to markerCount
  170.     theMarker = line i of labelString
  171.     markerFrame = marker(theMarker)
  172.     if theFrame < markerFrame then
  173.       return theMarker
  174.     end if
  175.   end repeat
  176. end
  177.